﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LIMS.SqlServerDAL
{
    /// <summary>
    ///  教师服务类
    ///  By：田玉粮
    ///  2013.3.19
    /// </summary>
    public class TeacherService : LIMS.IDAL.ITeacher
    {
        LIMSDataContext DB = new LIMSDataContext();

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="teacher"></param>
        /// <returns></returns>
        public Boolean Add(LIMS.Model.Teachers teacher)
        {
            TB_teacher teaTmp = new TB_teacher()
            {
                teacherId = teacher.teacherID,
                teacherName = teacher.teacherName,
                teacherPwd = teacher.teacherPwd
            };
            DB.TB_teacher.InsertOnSubmit(teaTmp);
            DB.SubmitChanges();
            return true;
        }

        /// <summary>
        /// 根据教师ID删除教师数据
        /// </summary>
        /// <param name="teacher"></param>
        /// <returns></returns>
        public Boolean Remove(LIMS.Model.Teachers teacher)
        {
            var teaTmp = DB.TB_teacher.SingleOrDefault(sg => sg.teacherId == teacher.teacherID);
            DB.TB_teacher.DeleteOnSubmit(teaTmp);
            DB.SubmitChanges();
            return true;
        }

        /// <summary>
        /// 根据教师ID获取其姓名
        /// </summary>
        /// <param name="teaID"></param>
        /// <returns></returns>
        public String FindName(String teaID)
        {
            var tmp = DB.TB_teacher.Where(w => w.teacherId == teaID).Single();
            return tmp.teacherName;
        }

        /// <summary>
        /// 根据教师ID获取该教师数据，返回null表示没有该教师数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public LIMS.Model.Teachers FindByID(String id)
        {
            Model.Teachers teacher = new Model.Teachers();
            var tmp = DB.TB_teacher.Where(w => w.teacherId == id).Single();
            if (tmp != null)
            {
                teacher.teacherID = tmp.teacherId;
                teacher.teacherName = tmp.teacherName;
                teacher.teacherPwd = tmp.teacherPwd;
            }
            return teacher;
        }

        /// <summary>
        /// 获取所有教师数据
        /// </summary>
        /// <returns></returns>
        public List<LIMS.Model.Teachers> FindAll()
        {
            List<LIMS.Model.Teachers> listTmp = new List<Model.Teachers>();
            DB.TB_teacher.Select(s => s).ToList().ForEach((item) =>
            {
                Model.Teachers teacher = new Model.Teachers()
                {
                    teacherID = item.teacherId,
                    teacherName = item.teacherName,
                    teacherPwd = item.teacherPwd
                };
                listTmp.Add(teacher);
            });
            return listTmp;
        }

        /// <summary>
        /// 根据教师姓名查找教师数据
        /// </summary>
        /// <param name="teaName"></param>
        /// <returns></returns>
        public List<LIMS.Model.Teachers> FindByName(String teaName)
        {
            List<LIMS.Model.Teachers> listTmp = new List<Model.Teachers>();
            DB.TB_teacher.Where(w => w.teacherName.Contains(teaName)).Select(s => s).ToList().ForEach((item) =>
            {
                Model.Teachers teacher = new Model.Teachers()
                {
                    teacherID = item.teacherId,
                    teacherName = item.teacherName,
                    teacherPwd = item.teacherPwd
                };
                listTmp.Add(teacher);
            });
            return listTmp;
        }

        /// <summary>
        /// 根据教师ID，修改教师信息
        /// </summary>
        /// <param name="teacher"></param>
        /// <returns></returns>
        public Boolean Modify(LIMS.Model.Teachers teacher)
        {
            var teaTmp = DB.TB_teacher.SingleOrDefault(sg => sg.teacherId == teacher.teacherID);
            teaTmp.teacherName = teacher.teacherName;
            teaTmp.teacherPwd = teacher.teacherPwd;
            DB.SubmitChanges();
            return true;
        }

    }

}
